<?php
namespace Tbf\Bundle\RouteBundle\Matcher;
use Symfony\Component\HttpFoundation\Request;
use Tbf\Bundle\RouteBundle\Matcher\MatcherInterface;
use Tbf\Component\Http\ServeFile\ServeFile;
use Tbf\Component\Path\PathFunction;

class DirMapMatcher implements MatcherInterface{
    protected $prefix_url;
    protected $prefix_file_path;
    function __construct($prefix_url,$prefix_file_path){
        $this->prefix_file_path = $prefix_file_path;
        $this->prefix_url = $prefix_url;
    }
    function match(Request $request){
        $path_info = $request->getPathInfo();
        if (strpos($path_info,$this->prefix_url)!==0){
            return null;
        }
        $remain_path = substr($path_info,strlen($this->prefix_url));
        $file_path = PathFunction::filePathJoin($this->prefix_file_path,$remain_path);
        list($resp,$err)  = ServeFile::serviceSymfony($request,$file_path);
        if ($err!==null){
            return null;
        }
        return $resp;
    }
}